<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Flex_seqs_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function insert($data){
        $this->db->query('call app_flex_seqs_ins(?,?,?,?,@id,@err,@msg)',$data);
        $query = $this->db->query("SELECT @err as err , @msg as msg, @id as id");
        return $query->result();
    }

    public function insertseg($data){
        $this->db->query('call app_flex_seq_segments_ins(?,?,?,?,?,?,?,?,?,@err,@msg)',$data);
        $query = $this->db->query("SELECT @err as err , @msg as msg");
        return $query->result();
    }

    public function updateseg($data){
        $this->db->query('call app_flex_seq_segments_upd(?,?,?,?,?,?,?,?,?,@err,@msg)',$data);
        $query = $this->db->query("SELECT @err as err , @msg as msg");
        return $query->result();
    }

    public function get_tables(){
        return $this->db->query('SHOW FULL TABLES FROM '.$this->db->database.' WHERE Table_Type = "BASE TABLE"')->result();
    }

    public function get_table_col($table_table){
        return $this->db->query("SHOW COLUMNS FROM ".$table_table." FROM ".$this->db->database)->result();
    }

    public function get_lookup_values($type){
        return $this->db->query('SELECT * FROM app_lookup_values WHERE lookup_type = "'.$type.'" ')->result();
    }

    public function get_app_seq(){
        return $this->db->query('SELECT * FROM app_sequence')->result();
    }

    public function get_field_name($id){
        return $this->db->query('SELECT * FROM app_flex_seqs WHERE flex_seq_id = '.$id)->result();
    }

    public function get_segments($id){
        return $this->db->query('SELECT * FROM app_flex_seq_segments WHERE flex_seq_id = "'.$id.'" ORDER BY segment_seq ASC')->result();
    }

    public function get_segments_by_id($id){
        return $this->db->query('SELECT * FROM app_flex_seq_segments WHERE segment_id = "'.$id.'" ')->result();
    }

    public function get_flex_seqs($tbl_name,$tbl_field){
        $id = 0;
        return $this->db->query('SELECT * FROM app_flex_seqs WHERE table_name = "'.$tbl_name.'" AND field_name = "'.$tbl_field.'" LIMIT 1 ')->result();
    }

    public function get_status($tbl,$fld){
        return $this->db->query('SELECT * FROM app_flex_seqs WHERE table_name LIKE "'.$tbl.'" OR field_name LIKE "'.$fld.'"')->result();
    }

    public function get_flex_seqs_id($id){
        return $this->db->query('SELECT * FROM app_flex_seqs WHERE flex_seq_id = "'.$id.'" ')->result();
    }

    public function get_sequence($seqname){
        return $this->db->query('SELECT * FROM app_sequence WHERE seq_name = "'.$seqname.'" ')->result();
    }

    public function del_seg($data){
        $this->db->query('call app_flex_seq_segments_del(?,@err,@msg)',$data);
        $query = $this->db->query("SELECT @err as err , @msg as msg");
        return $query->result();
    }

    public function get_account($id){
        return $this->db->query('SELECT * FROM ar_account WHERE account_id ="'.$id.'" ')->result();
    }
}
